import { C42ShopItemList } from "@/components";
import { ApiShopItemsMapping } from "@/data/shop/shopTypes";
import {
  getShopItemsOfType,
  useApplicationInfo,
} from "@/store/applicationDataContext";
import { useEffect } from "react";
import { ActionNames, sendAction } from "@/utils/tracking";
import { useDidShow } from "@tarojs/taro";
import { DiscoverTabsEnum } from "@/pages/discover/discoverData";
import style from "./index.module.scss";

export default function C18PublicationListing() {
  const { applicationState, dispatch } = useApplicationInfo();
  const { publications } = applicationState;

  useEffect(() => {
    if (!applicationState.publications.length) {
      getShopItemsOfType(ApiShopItemsMapping.Publication, dispatch);
    }
    initSharing();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [dispatch]);

  useEffect(() => {
    sendAction(ActionNames.PublicationImpression);
  }, []);

  useDidShow(() => {
    initSharing();
  });

  const initSharing = () => {
    if (dispatch) {
      dispatch({
        type: "DISCOVER_TAB",
        data: {
          tab: DiscoverTabsEnum.Publications,
        },
      });
    }
  };

  return (
    <C42ShopItemList shopItemList={publications} cardType="product-card" />
  );
}
